package com.jason.practice.service.impl;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.jason.practice.model.CitizenLoanInfo;
import com.jason.practice.model.FinanceServiceInfo;
import com.jason.practice.mapper.FinanceServiceInfoMapper;
import com.jason.practice.response.Response;
import com.jason.practice.service.FinanceServiceInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jason.practice.utils.ExcelUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiayb_9169@126.com
 * @since 2022-10-23
 */
@Service
public class FinanceServiceInfoServiceImpl extends ServiceImpl<FinanceServiceInfoMapper, FinanceServiceInfo> implements FinanceServiceInfoService {
    @Override
    public Response uploadExcel(File file) {
        List<FinanceServiceInfo> t0079JList = new ArrayList<>();
        try {
            ImportParams im = new ImportParams();
            im.setTitleRows(3);
            im.setHeadRows(1);
            im.setStartSheetIndex(2);
            im.setReadRows(4);
            t0079JList = ExcelUtil.parseExcel(file, FinanceServiceInfo.class, im);

            int serviceNum = 0;
            int coverCustomNum = 0;
            BigDecimal profitPrice = new BigDecimal(0.00);
            for (FinanceServiceInfo t0079J : t0079JList) {
                t0079J.setExcelName(file.getName());
                t0079J.setSheetName("优化新市民基础金融服务情况统计表");

                if (!StringUtils.isBlank(t0079J.getServiceNum())) {
                    serviceNum += NumberUtils.toInt(t0079J.getServiceNum());
                }
                if (!StringUtils.isBlank(t0079J.getCoverCustomNum())) {
                    coverCustomNum += NumberUtils.toInt(t0079J.getCoverCustomNum());
                }
                if (!StringUtils.isBlank(t0079J.getProfitPrice())) {
                    BigDecimal loanBalanceDecimal = new BigDecimal(t0079J.getProfitPrice());
                    profitPrice = profitPrice.add(new BigDecimal(loanBalanceDecimal.toString()));
                }
            }
            FinanceServiceInfo total = new FinanceServiceInfo();
            total.setServiceNum(serviceNum+"");
            total.setCoverCustomNum(coverCustomNum+"");
            total.setProfitPrice(profitPrice.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()+"");
//            saveBatch(t0079JList);
            ExcelUtil.editExcel(file, total);
        } catch (Exception e) {
            log.error("BaseCommonServiceImpl error:", e);
        }
        return Response.ok(t0079JList);
    }
}
